package org.gwu.seuss.admin.commandline;

import org.gwu.seuss.ui.commandline.BaseCommandLineArgs;
import org.gwu.seuss.ui.validator.RequestValidator;

public class AdminValidator extends RequestValidator {

	@Override
	public <T extends BaseCommandLineArgs> boolean isValid(T cli) {
		AdministratorCommandLineArgs cla = (AdministratorCommandLineArgs) cli;
		if (tooManyArguments(cla)) {
			errorMessage = "too many arguments";
			return false;
		}
		if (invalidCreateUserCommand(cla)) {
			errorMessage = "new user requires login name and password";
			return false;
		}
		
		if(cla.getNewLoginName()!= null){
			return isValidLogin(cla.getLogin(), "new user's login name has invalid format");
		}
		
		return true;
	}

	private boolean invalidCreateUserCommand(AdministratorCommandLineArgs cla){
	
		//Have a password but no username
		boolean invalidCase1 = cla.isSetNewPassword() == true && cla.getNewLoginName() == null;
		
		//Do not have a password and have a username
		boolean invalidCase2 = cla.isSetNewPassword() == false && cla.getNewLoginName() != null;
		
		return invalidCase1 || invalidCase2;
	}

	private boolean tooManyArguments(AdministratorCommandLineArgs cla) {

		if (cla.getDisableUsername() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getEnableUsername() != null
						|| cla.isSetNewPassword() || cla.getNewLoginName() != null)) {
			return true;
		} else if (cla.getEnableUsername() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null
						|| cla.isSetNewPassword() || cla.getNewLoginName() != null)) {
			return true;
		} else if (cla.isSetNewPassword() 
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else if (cla.getNewLoginName() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else if (cla.getRemoveUsername() != null
				&& (cla.getNewLoginName() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else {
			return false;
		}
	}
}
